//
//  NetworkTools.swift
//  ZHTQWeibo
//
//  Created by ZHTQ on 2018/9/17.
//  Copyright © 2018年 ZHTQ. All rights reserved.
//

import UIKit

class NetworkTools: AFHTTPSessionManager {
    
    static let sharedInstance: NetworkTools = {
        guard let baseURL = URL(string: "https://api.weibo.com/") else {
            return NetworkTools(sessionConfiguration: URLSessionConfiguration.default)
        }
        let instance = NetworkTools(baseURL: baseURL, sessionConfiguration: URLSessionConfiguration.default)
        return instance
    }()
    
    //MARK:-  外部控制方法
    /// 加载数据
    func loadStatusData(since_id: String?, max_id: String?, finished: @escaping (_ arrayObject: Any?, _ error: Error?) -> ()) {
        
        assert(UserAccount.isLogin() != false, "必须登录获取授权")
        
        let path = "2/statuses/home_timeline.json"
        let maxId = max_id != "0" ? "\(Int(max_id!)! - 1)"  : "0"
        
        let parameters = ["access_token": UserAccount.loadUserAccount()?.access_token!, "since_id": since_id, "max_id": maxId]
        
        get(path, parameters: parameters, progress: nil, success: { (task, obj) in
            
            guard let array = obj else {
                return
            }
            
            finished(array, nil)
            
        }) { (task, error) in
            
            finished(nil, error)
        }
       

    }
}
